<div class="alert-bg"></div>

<div class="add-cart">
<form name="buy" action="" method="GET" onSubmit="return goods_buy(this)">

        <input type="hidden" name="buyType" value="buyNow">
        <input type="hidden" name="goods" value="<{$goods.id}>">
        <input type="hidden" name="attr" id="frm-attr-id" value="0">

        <em class="close" onclick="show_cart(0)"></em>

        <div class="goods-info">
            <div class="left">
                <img src="<{$goods.thumbsrc}>" class="img" alt="">
            </div>
            <div class="middle">
                <p id="goods-buy-price" class="money"><{$goods.price}></span>
                <p id="goods-buy-quantity" class="stock"><{$goods.quantity}></span>
            </div>
        </div>

        <div id="goods-attrs">
        <{foreach from=$goods.attr.attrs item="attr"}>
	        <div class="goods-sort attrs">
	            <p class="title"><{$attr.name}></p>
	            <ul class="filter fix">
                <{foreach from=$attr.list item="sattr"}>
	                <li class="li"><span attrId="<{$sattr.id}>"><{$sattr.name}></span></li>
                <{/foreach}>
	            </ul>
	        </div>
        <{/foreach}>
        </div>

        <div class="goods-number fix">
            <span class="left">购买数量</span>
            <p class="right fix">
                <em class="less" onclick="cartAddQuantity(this, -1)"></em>
                <input type="number" class="number" name="count" id="goods-buy-count" value="1">
                <em class="add" onclick="cartAddQuantity(this, 1)"></em>
            </p>
        </div>
        <input type="hidden" name="tuanId" value="<{$buyTuanId}>">
        <input type="submit" name="submit_op" class="ok" value="确 认">
</form>


    <div id="cart_message" style="display: none;">
        <p class="title">提示框</p>
        <p class="jiaru">商品已添加到购物车</p>
        <p class="content fix">
            <a class="chakan" href="/m/cart">查看购物车</a>
            <span class="jixu" onclick="$.unblockUI()">继续购物</span>
        </p>
    </div>


</div>


<script>

function show_cart(show) {
    if (show)
    {
        $('.add-cart').fadeIn();
        $('.alert-bg').fadeIn();
        document.body.style.overflow = 'hidden';
    }
    else
    {
        $('.add-cart').fadeOut();
        $('.alert-bg').fadeOut();
        document.body.style.overflow = 'auto';
    }
}

function cartAddQuantity (item, n) {
    var currN = $(item).parent().find('input').val();
    var nextN = parseInt(currN) + n;

    if (nextN > 100)
        nextN = 100;

    if (nextN <= 0)
        nextN = 1;

    $(item).parent().find('input').val(nextN);
    var ipt = $(item).parent().find('input')[0];
    //cart_quantity_changed(ipt.form);
}

// JS购物车变量
<{if $goods.attr}>
var goodsAttrValues = {
    <{foreach from=$goods.attr.values item="val"}>
        '<{$val.key}>' : { 'price' : <{$val.price}>, 'quantity': <{$val.quantity}>, 'id' : <{$val.id}> },
    <{/foreach}>
};
<{else}>
var goodsAttrValues = null;
<{/if}>

function goods_building_price_with_quantity ()
{
    var currentKey = '';
    $('#goods-attrs .attrs').each(function () {
        var x = $(this).find('span.active:first');
        if (x.length == 1)
            currentKey += '|' + x.attr('attrId');
    });

    if (goodsAttrValues.hasOwnProperty(currentKey))
    {
        var info = goodsAttrValues[currentKey];
        $('#goods-buy-price').html('￥' + info.price);
        $('#goods-buy-quantity').html('库存:' + info.quantity + '件');
        $('#frm-attr-id').val(info.id);
        $('input[name="submit_op"]').removeAttr("disabled");
    }
    else
    {
        $('input[name="submit_op"]').attr("disabled", "disabled");
        $('#goods-buy-price').html('无效规格');
        $('#goods-buy-quantity').html('');
        $('#frm-attr-id').val(0);
    }
}

function goods_show_buy_box(buyType, title)
{
    var form = document.forms['buy'];
    form.buyType.value = buyType;
    form.submit_op.value = title;

    show_cart(1);
}

function goods_buy(formItem)
{
    // quantity > 0
    // attr id 有效性检查

    var type = formItem.buyType.value;
    if (type == 'buyNow')
    {
        formItem.action = '/m/buy/now';
        return true;
    }
    else if (type == 'addCart')
    {
        show_cart(0);

        goods = formItem.goods.value;
        attr  = formItem.attr.value;
        count = formItem.count.value;

        url = '/m/cart/add?goods=' + goods
            + '&attr=' + attr
            + '&count=' + count;

        $.get(url, function (o) {
            if (o.status == 0)
            {
                $.blockUI({
                    message : $('#cart_message'),
                    css : { backgroundColor: '#f00', color: '#eee' },
                    onOverlayClick: $.unblockUI
                });
            }else if(o.status == 2){
                window.location.href="/login"; 
            } else {
               $.blockUI({message: '添加错误!'});
            }

        }, 'json');

        return false;
    }
    else if (type == 'buyTuan')
    {
        formItem.action = '/m/buy/tuan';
        return true;
    }
    else if (type == 'gift')
    {
        formItem.action = '/m/buy/gift';
        return true;
    }



    return false;
}

$(document).ready(function () {
    $('#goods-attrs .attrs span').each(function () {
        $(this).click(function () {
            $(this).parent().parent().find('span').removeClass('active');
            $(this).addClass('active');

            goods_building_price_with_quantity();
        });
    });


    if (JSON.stringify(goodsAttrValues) == '{}' || goodsAttrValues == null)
    {
        // 商品属性空
    }
    else
    {
        // 未选择属性前，不可点击下一步
        $('input[name="submit_op"]').attr("disabled", "disabled");
        $('#goods-buy-price').html('请选择商品规格');
    }
});


</script>
